var util = require('../../../../utils/util.js')

var apiHost = wx.getStorageSync("apiHost"); //请求地址
var Appid = wx.getStorageSync('Appid');
var Secret = wx.getStorageSync('Secret');

var AppidFlag = wx.getStorageSync('AppidFlag');

Page({

  /**
   * 页面的初始数据
   */
  data: {
    startTime: '开始时间',    //开始时间
    endTime: '结束时间',      //结束时间
    level: 'one',                               //等级
    teamList : '',                              //团队集合
    userPhone : '',                             //用户手机号
    userName : '',                              //用户姓名
    field: '',                                  //排序字段
    sort: '',                                   //倒序
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that = this;
    var token = wx.getStorageSync('token');      //用户的token信息
    var level = that.data.level;

    that.setData({
      token : token
    })
    
    that.getTeam(token, level, 0, 15, "", "", "", "", "", "");  //获取团队信息
    that.getHeight(); //计算高度
  },

  /**
   * 头部切换 一级二级好友
   */
  tabLevel:function(e){
    var that = this;
    var level = e.currentTarget.dataset.level;
    var token = that.data.token;
    that.setData({
      level: level,
      userPhone: '',
      userName: '',
      field: '',
      sort: '',
      startTime: '',
      endTime: '',
    })

    that.getTeam(token, level, 0, 15, "", "", "", "", "", "");  //获取团队信息
  },

  /**
   * 获取input框值 （判断是用户名、手机号）
   */
  searchValue :function(e){
    var that = this;
    var inputValue = e.detail.value;
    var isRealNum = that.isRealNum(inputValue);

    var userPhone = '';
    var userName = '';

    if (isRealNum){
      userPhone = inputValue;
    }else{
      userName = inputValue;
    }

    that.setData({
      userPhone: userPhone,
      userName: userName
    });
  },

  /**
   *  团队查询按钮
   */
  teamCheck:function(){
    var that = this;
    var token = that.data.token;
    var level = that.data.level;
    var startTime = that.data.startTime; 
    var endTime = that.data.endTime;
    var userPhone = that.data.userPhone;
    var userName = that.data.userName;

    that.getTeam(token, level, 0, 15, '', '', startTime, endTime, userPhone, userName);
  },

  /**
   * 团队列表排序
   */
  teamPx:function(e){
    var that = this;
    var field = e.currentTarget.dataset.field;

    that.setData({
      field: field,
      sort: 'desc'
    });
    var token = that.data.token;
    var level = that.data.level;
    var startTime = that.data.startTime;
    var endTime = that.data.endTime;
    var userPhone = that.data.userPhone;
    var userName = that.data.userName;
    var field = that.data.field;
    var sort = that.data.sort;

    that.getTeam(token, level, 0, 15, field, sort, startTime, endTime, userPhone, userName);
  },

  /**
   * 获取团队信息
   */
  getTeam: function (token, level, start, length, field, sort, startTime, endTime, userPhone, userName){
    var that = this;

    wx.showLoading({
      title: '加载中',
      icon: 'loading',
    });

    wx.request({
      url: apiHost + "/mobile/users/team/list", //仅为示例，并非真实的接口地址
      data: {
        token: token,
        appId: AppidFlag,
        level: level,
        start: start,
        length: length,
        field: field,
        sort: sort,
        startTime: startTime,
        endTime: endTime,
        userPhone: userPhone,
        userName: userName
      },
      header: {
        'content-type': 'application/json' // 默认值
      },
      success: function (res) {
        var code = res.data.code;
        var msg = res.data.msg;

        if (code == "10000") {
          var teamList = res.data.data.list
          that.ordersPaging(start,teamList);
        } else {
          that.showMsg(msg);
        }
        wx.hideLoading();   //隐藏加载loading
      }
    })
  },

  /**
   * 下拉
   */
  scrollLower: function () {
    var that = this;
    var token = that.data.token;
    var level = that.data.level;
    var start = that.data.startFlag;
    var field = that.data.field;
    var sort = that.data.sort;
    var startTime = that.data.startTime;
    var endTime = that.data.endTime;
    var userPhone = that.data.userPhone;
    var userName = that.data.userName;

    that.getTeam(token, level, start, 15, field, sort, startTime, endTime, userPhone, userName);
  },

  /**
   * 商品分页
   */
  ordersPaging: function (start, teamData) {
    var that = this;
    if (start > 0) {
      var teamList = that.data.teamList;
      // 追加数据
      for (var i = 0; i < teamData.length; i++) {
        teamList.push(teamData[i])
      }
      start = parseInt(start) + 1;
    } else {
      var teamList = teamData;
      start = 1;
    }
    console.log(teamList)
    that.setData({
      teamList: teamList,
      startFlag: start
    })
  },

  /**
   *  单项高度
   */
  getHeight: function () {
    var that = this;
    var query = wx.createSelectorQuery();

    var reportDetailTopConHeight = "";
    var tableThConHeight = "";

    query.select('#reportDetailTopCon').boundingClientRect()
    query.select('#tableThCon').boundingClientRect()
    

    query.exec(function (res) {
      reportDetailTopConHeight = res[0].height;
      tableThConHeight = res[1].height;
      wx.getSystemInfo({
        success: function (res) {
          that.setData({
            proHeight: res.windowHeight - reportDetailTopConHeight - tableThConHeight -30
          });
        }
      });
    });
  },
  
  /**
   * 开始时间
   */
  bindStartDateChange: function (e) {
    this.setData({
      startTime: e.detail.value
    })
  },

  /**
   * 结束时间
   */
  bindEndDateChange: function (e) {
    this.setData({
      endTime: e.detail.value
    })
  },

  /**
   * 跳转邀请记录
   */
  goInvitationRecord:function(){
    wx.navigateTo({
      url: "../invitationRecord/invitationRecord",
    })
  },

  /**
   * 跳转个人 报表
   */
  goPersonalR:function(e){
    var that= this;
    var userid = e.currentTarget.dataset.userid
    wx.navigateTo({
      url: "../personalReport/personalReport?userid=" + userid,
    })
  },


  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 提示信息
   */
  showMsg: function (msg) {
    wx.showToast({
      title: msg,
      icon: 'none',
      duration: 2000,
      mask: true
    });
  },

  /**
   * 判断是否为数字
   */
  isRealNum : function (val){
    // isNaN()函数 把空串 空格 以及NUll 按照0来处理 所以先去除
    if(val === "" || val == null){
      return false;
    }
    if (!isNaN(val)) {
      return true;
    } else {
      return false;
    }
  },
})